﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Himp.Platform.Migrations
{
    /// <inheritdoc />
    public partial class modify_table_TopicDict : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_EChartConfig_MainTopicConfig_ProjectConfigId",
                table: "EChartConfig");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdApiConfig_MainTopicConfig_TopicConfigId",
                table: "MainStdApiConfig");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdDataModelDict_MainTopicConfig_TopicConfigId",
                table: "MainStdDataModelDict");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdEventModelDict_MainTopicConfig_TopicConfigId",
                table: "MainStdEventModelDict");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdTableDict_MainTopicConfig_TopicConfigId",
                table: "MainStdTableDict");

            migrationBuilder.DropForeignKey(
                name: "FK_StdFormDict_MainTopicConfig_ProjectConfigId",
                table: "StdFormDict");

            migrationBuilder.DropTable(
                name: "MainTopicConfig");

            migrationBuilder.DropTable(
                name: "MainNamespaceConfig");

            migrationBuilder.DropPrimaryKey(
                name: "PK_StdFormDict",
                table: "StdFormDict");

            migrationBuilder.DropColumn(
                name: "SfCodg",
                table: "StdFormDict");

            migrationBuilder.RenameTable(
                name: "StdFormDict",
                newName: "MainStdFormDict");

            migrationBuilder.RenameColumn(
                name: "SfName",
                table: "MainStdFormDict",
                newName: "FormName");

            migrationBuilder.RenameColumn(
                name: "ProjectConfigId",
                table: "MainStdFormDict",
                newName: "TopicConfigId");

            migrationBuilder.RenameIndex(
                name: "IX_StdFormDict_ProjectConfigId",
                table: "MainStdFormDict",
                newName: "IX_MainStdFormDict_TopicConfigId");

            migrationBuilder.AddPrimaryKey(
                name: "PK_MainStdFormDict",
                table: "MainStdFormDict",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "MainNamespaceDict",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    NamespaceCode = table.Column<string>(type: "text", nullable: false),
                    NamespaceName = table.Column<string>(type: "text", nullable: false),
                    DefaultFlag = table.Column<bool>(type: "boolean", nullable: false),
                    Srtno = table.Column<int>(type: "integer", nullable: false),
                    Exp = table.Column<string>(type: "text", nullable: true),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    CreationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
                    LastModificationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    LastModifierId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainNamespaceDict", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "MainTopicDict",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    TopicCode = table.Column<string>(type: "text", nullable: false),
                    TopicName = table.Column<string>(type: "text", nullable: false),
                    Exp = table.Column<string>(type: "text", nullable: true),
                    NamespaceDictId = table.Column<Guid>(type: "uuid", nullable: false),
                    ParentId = table.Column<Guid>(type: "uuid", nullable: true),
                    Srtno = table.Column<int>(type: "integer", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainTopicDict", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MainTopicDict_MainNamespaceDict_NamespaceDictId",
                        column: x => x.NamespaceDictId,
                        principalTable: "MainNamespaceDict",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_MainTopicDict_MainTopicDict_ParentId",
                        column: x => x.ParentId,
                        principalTable: "MainTopicDict",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_MainTopicDict_NamespaceDictId",
                table: "MainTopicDict",
                column: "NamespaceDictId");

            migrationBuilder.CreateIndex(
                name: "IX_MainTopicDict_ParentId",
                table: "MainTopicDict",
                column: "ParentId");

            migrationBuilder.AddForeignKey(
                name: "FK_EChartConfig_MainTopicDict_ProjectConfigId",
                table: "EChartConfig",
                column: "ProjectConfigId",
                principalTable: "MainTopicDict",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdApiConfig_MainTopicDict_TopicConfigId",
                table: "MainStdApiConfig",
                column: "TopicConfigId",
                principalTable: "MainTopicDict",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdDataModelDict_MainTopicDict_TopicConfigId",
                table: "MainStdDataModelDict",
                column: "TopicConfigId",
                principalTable: "MainTopicDict",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdEventModelDict_MainTopicDict_TopicConfigId",
                table: "MainStdEventModelDict",
                column: "TopicConfigId",
                principalTable: "MainTopicDict",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdFormDict_MainTopicDict_TopicConfigId",
                table: "MainStdFormDict",
                column: "TopicConfigId",
                principalTable: "MainTopicDict",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdTableDict_MainTopicDict_TopicConfigId",
                table: "MainStdTableDict",
                column: "TopicConfigId",
                principalTable: "MainTopicDict",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_EChartConfig_MainTopicDict_ProjectConfigId",
                table: "EChartConfig");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdApiConfig_MainTopicDict_TopicConfigId",
                table: "MainStdApiConfig");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdDataModelDict_MainTopicDict_TopicConfigId",
                table: "MainStdDataModelDict");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdEventModelDict_MainTopicDict_TopicConfigId",
                table: "MainStdEventModelDict");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdFormDict_MainTopicDict_TopicConfigId",
                table: "MainStdFormDict");

            migrationBuilder.DropForeignKey(
                name: "FK_MainStdTableDict_MainTopicDict_TopicConfigId",
                table: "MainStdTableDict");

            migrationBuilder.DropTable(
                name: "MainTopicDict");

            migrationBuilder.DropTable(
                name: "MainNamespaceDict");

            migrationBuilder.DropPrimaryKey(
                name: "PK_MainStdFormDict",
                table: "MainStdFormDict");

            migrationBuilder.RenameTable(
                name: "MainStdFormDict",
                newName: "StdFormDict");

            migrationBuilder.RenameColumn(
                name: "TopicConfigId",
                table: "StdFormDict",
                newName: "ProjectConfigId");

            migrationBuilder.RenameColumn(
                name: "FormName",
                table: "StdFormDict",
                newName: "SfName");

            migrationBuilder.RenameIndex(
                name: "IX_MainStdFormDict_TopicConfigId",
                table: "StdFormDict",
                newName: "IX_StdFormDict_ProjectConfigId");

            migrationBuilder.AddColumn<string>(
                name: "SfCodg",
                table: "StdFormDict",
                type: "text",
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddPrimaryKey(
                name: "PK_StdFormDict",
                table: "StdFormDict",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "MainNamespaceConfig",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Contdesc = table.Column<string>(type: "text", nullable: true),
                    DefaultFlag = table.Column<bool>(type: "boolean", nullable: false),
                    NamespaceName = table.Column<string>(type: "text", nullable: false),
                    Srtno = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainNamespaceConfig", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "MainTopicConfig",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    NamespaceConfigId = table.Column<Guid>(type: "uuid", nullable: false),
                    ParentId = table.Column<Guid>(type: "uuid", nullable: true),
                    Contdesc = table.Column<string>(type: "text", nullable: true),
                    Srtno = table.Column<int>(type: "integer", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    TopicCodg = table.Column<string>(type: "text", nullable: false),
                    TopicName = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainTopicConfig", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MainTopicConfig_MainNamespaceConfig_NamespaceConfigId",
                        column: x => x.NamespaceConfigId,
                        principalTable: "MainNamespaceConfig",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_MainTopicConfig_MainTopicConfig_ParentId",
                        column: x => x.ParentId,
                        principalTable: "MainTopicConfig",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_MainTopicConfig_NamespaceConfigId",
                table: "MainTopicConfig",
                column: "NamespaceConfigId");

            migrationBuilder.CreateIndex(
                name: "IX_MainTopicConfig_ParentId",
                table: "MainTopicConfig",
                column: "ParentId");

            migrationBuilder.AddForeignKey(
                name: "FK_EChartConfig_MainTopicConfig_ProjectConfigId",
                table: "EChartConfig",
                column: "ProjectConfigId",
                principalTable: "MainTopicConfig",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdApiConfig_MainTopicConfig_TopicConfigId",
                table: "MainStdApiConfig",
                column: "TopicConfigId",
                principalTable: "MainTopicConfig",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdDataModelDict_MainTopicConfig_TopicConfigId",
                table: "MainStdDataModelDict",
                column: "TopicConfigId",
                principalTable: "MainTopicConfig",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdEventModelDict_MainTopicConfig_TopicConfigId",
                table: "MainStdEventModelDict",
                column: "TopicConfigId",
                principalTable: "MainTopicConfig",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MainStdTableDict_MainTopicConfig_TopicConfigId",
                table: "MainStdTableDict",
                column: "TopicConfigId",
                principalTable: "MainTopicConfig",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_StdFormDict_MainTopicConfig_ProjectConfigId",
                table: "StdFormDict",
                column: "ProjectConfigId",
                principalTable: "MainTopicConfig",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
